package blog.action;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import blog.entity.Article;
import blog.entity.Comment;
import blog.entity.User;
import blog.service.ArticleService;
import blog.service.CategoryService;
import blog.service.CommentService;
import blog.service.TagService;
import blog.service.UserService;

/**
 * 文章
 */
@Controller
public class ArticleAction {

    @Resource
    private ArticleService articleService;

    @Resource
    private CommentService commentService;

    @Resource
    private UserService userService;

    @Resource
    private TagService tagService;

    @Resource
    private CategoryService categoryService;

    /**
     * 文章详情页显示
     */
    @RequestMapping(value = "/article/{articleId}")
    public String getArticleDetailPage(@PathVariable("articleId") Integer articleId, ModelMap m) {

        //文章信息
        Article article = articleService.getArticleById(articleId);
        if (article == null) {
            return "404";
        }
        m.put("article", article);

        //用户信息
        User user = userService.getUserById(article.getArticleUserId());
        article.setUser(user);

        //评论列表
        List<Comment> commentList = commentService.listCommentByArticleId(articleId);
        m.put("commentList", commentList);
        
        //相关文章, 查询出和该文章小分类相同的5条文章
        int categoryId=article.getCategoryList().get(1).getCategoryId();
        List<Article> similarArticleList = articleService.listArticleByCategoryId(categoryId, 5);
        m.put("similarArticleList", similarArticleList);

        //猜你喜欢
        List<Article> mostViewArticleList = articleService.listArticleByViewCount(5);
        m.put("mostViewArticleList", mostViewArticleList);

        //获取下一篇文章
        Article afterArticle = articleService.getAfterArticle(articleId);
        m.put("afterArticle", afterArticle);

        //获取上一篇文章
        Article preArticle = articleService.getPreArticle(articleId);
        m.put("preArticle", preArticle);
        
        //获得随机文章
        List<Article> randomArticleList = articleService.listRandomArticle(8);
        m.put("randomArticleList", randomArticleList);
        
        //获得热评文章
        List<Article> mostCommentArticleList = articleService.listArticleByCommentCount(8);
        m.put("mostCommentArticleList", mostCommentArticleList);

        return "Pages/article-detail";
    }

    /**
     * 文章访浏览次数增加
     * @param id 文章ID
     * @return 访问量数量
     */
    @ResponseBody  @RequestMapping(value = "/article/view/{id}", method = {RequestMethod.POST})
    public String increaseViewCount(@PathVariable("id") Integer id) {
        Article article = articleService.getArticleById(id);
        Integer newViewCount = article.getArticleViewCount() + 1;
        article.setArticleViewCount(newViewCount);
        articleService.updateArticle(article);
        return newViewCount+"";
    }

    /**
     * 点赞增加
     * @param id 文章ID
     * @return 点赞量数量
     */
    @ResponseBody  @RequestMapping(value = "/article/like/{id}", method = {RequestMethod.POST})
    public String increaseLikeCount(@PathVariable("id") Integer id) {
        Article article = articleService.getArticleById(id);
        Integer newLikeCount = article.getArticleLikeCount() + 1;
        article.setArticleLikeCount(newLikeCount);
        articleService.updateArticle(article);
        return newLikeCount+"";
    }

}
